# 导入 Appium 远程驱动
from appium import webdriver
# 导入 Android 自动化引擎的配置类（UiAutomator2Options）
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.common.by import By


class TestEleOper:
    def setup_class(self):
        # Capability 设置定义为字典
        caps = {
            # 指定测试的目标平台为Android（可选值：Android、iOS）
            "platformName": "Android",
            # 指定测试设备的唯一标识符（Android设备序列号，可通过adb devices命令获取）
            "appium:deviceName": "3f743cff",
            # 指定Android自动化测试使用的引擎为UiAutomator2（Appium推荐，支持Android 5.0+）
            "appium:automationName": "UiAutomator2",
            # 设置测试前后是否重置应用状态：true表示不重置（保留用户数据、登录状态等）
            "appium:noReset": "true"
        }
        # 初始化 driver
        # appium_server_url = 'http://192.168.31.155:4723'
        # appium_server_url = 'http://172.16.104.44:4723'
        appium_server_url = 'http://127.0.0.1:4723'
        self.driver = webdriver.Remote(
            appium_server_url,
            options=UiAutomator2Options().
            load_capabilities(caps)
        )
        # 设置隐式等待
        self.driver.implicitly_wait(5)

    def teardown_class(self):
        self.driver.quit()

    def test_id(self):
        ele = self.driver.find_element(AppiumBy.ID, "android:id/text1")
        print(ele)

    def test_aid(self):
        ele = self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, "App")
        print(ele)

    def test_xpath(self):
        ele = self.driver.find_element(AppiumBy.XPATH, "//*[@text='App']")
        print(ele)

    def test_xpath1(self):
        ele = self.driver.find_element(AppiumBy.XPATH, "//*[@text='App' and @resource-id='android:id/text1']")
        print(ele)

    def test_scroll_ele(self):
        ele_start = self.driver.find_element(AppiumBy.ACCESSIBILITY_ID,"Animation")
        ele_end = self.driver.find_element(AppiumBy.ACCESSIBILITY_ID,"Grid")
        self.driver.scroll(ele_end, ele_start,2000)
